package com.zhongjian.technologycal.mall.activity.personal.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.githang.statusbar.StatusBarCompat;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.zhongjian.technologycal.mall.R;
import com.zhongjian.technologycal.mall.base.BaseActivity;
import com.zhongjian.technologycal.mall.base.BaseContacts;
import com.zhongjian.technologycal.mall.bean.WalletBean;
import com.zhongjian.technologycal.mall.net.HttpRetrofitClient;
import com.zhongjian.technologycal.mall.utils.NetWorkUtils;
import com.zhongjian.technologycal.mall.utils.ToastUitl;
import com.zhongjian.technologycal.mall.utils.UserUtils;
import com.zhongjian.technologycal.mall.view.NumberRunningTextView;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.ResponseBody;
import rx.Subscriber;

/**
 * Created by wei on 2017/12/20.
 * 我的钱包实现页面
 */

public class MineMallWalletActivity extends BaseActivity {
    @BindView(R.id.wallet_tv_title)
    TextView wallet_tv_title;
    @BindView(R.id.wallet_tv_cash)
    NumberRunningTextView wallet_tv_cash;
    @BindView(R.id.wallet_tv_shopping)
    NumberRunningTextView wallet_tv_shopping;
    @BindView(R.id.wallet_tv_pash)
    NumberRunningTextView wallet_tv_pash;
    @BindView(R.id.wallet_tv_credits)
    NumberRunningTextView wallet_tv_credits;
    @BindView(R.id.tv_total)
    NumberRunningTextView tv_total;
    @BindView(R.id.tv_shopping_money)
    NumberRunningTextView tv_shopping_money;
    @BindView(R.id.wallet_tv_nickname)
    TextView wallet_tv_nickname;
    @BindView(R.id.wallet_iv_img)
    CircleImageView wallet_iv_img;
    @BindView(R.id.wallet_iv_vip0)
    ImageView wallet_iv_vip0;
    @BindView(R.id.wallet_iv_vip1)
    ImageView wallet_iv_vip1;
    @BindView(R.id.wallet_iv_vip2)
    ImageView wallet_iv_vip2;
    @BindView(R.id.wallet_iv_vip3)
    ImageView wallet_iv_vip3;
    @BindView(R.id.wallet_iv_vip4)
    ImageView wallet_iv_vip4;
    @BindView(R.id.wallet_iv_line0)
    ImageView wallet_iv_line0;
    @BindView(R.id.wallet_iv_line1)
    ImageView wallet_iv_line1;
    @BindView(R.id.wallet_iv_line2)
    ImageView wallet_iv_line2;
    @BindView(R.id.wallet_iv_line3)
    ImageView wallet_iv_line3;
    private String strMoney;
    @BindView(R.id.wallet_tv_time)
    TextView wallet_tv_time;

    private String userName ;

    @Override
    protected void beforeInit() {
        super.beforeInit();
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.blue), true);
//        StatusBarUtil.setColorNoTranslucent(this, getResources().getColor(R.color.blue));
//        StatusBarUtil.setMiuiStatusBarDarkMode(this, false);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_mymall_wallet;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        wallet_tv_title.setText("我的钱包");
        initViewRequest();
    }

    @Override
    public void initPresenter() {

    }

    private void initViewRequest() {
        if (!NetWorkUtils.isNetConnected(getBaseContext())) {
            ToastUitl.showShort(R.string.no_net);
            return;
        }
        HttpRetrofitClient.getInstance(getBaseContext())
                .getRequest(BaseContacts.URL_PERSONAL_WALLET + UserUtils.getUserToken(),
                        new Subscriber<ResponseBody>() {
                            @Override
                            public void onStart() {
                                super.onStart();
//                                LoadingDialog.showDialogForLoading(MineMallWalletActivity.this);
                            }

                            @Override
                            public void onCompleted() {
//                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onError(Throwable e) {
//                                LoadingDialog.cancelDialogForLoading();
                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
//                                LoadingDialog.cancelDialogForLoading();

                                try {
                                    Gson gson = new Gson();
                                    WalletBean walletBean = gson.fromJson(responseBody.string(), WalletBean.class);
                                    if (walletBean.getError_code() == 0) {
                                        wallet_tv_shopping.setContent(""+ walletBean.getData().getPersonDataMap().getRemainPoints());
//                                        wallet_tv_shopping.setText("" + walletBean.getData().getPersonDataMap().getRemainPoints());//购物币
                                        wallet_tv_cash.setContent("" + walletBean.getData().getPersonDataMap().getRemainElecNum());//现金币
                                        wallet_tv_nickname.setText(walletBean.getData().getPersonDataMap().getNickName());
                                        wallet_tv_credits.setContent("" + walletBean.getData().getPersonDataMap().getRemainVIPAmount());//消费积分
                                        tv_total.setContent("" + walletBean.getData().getPersonDataMap().getTotalCost());
                                        tv_shopping_money.setContent("" + walletBean.getData().getPersonDataMap().getRemainStream());
                                        wallet_tv_pash.setContent("" + walletBean.getData().getPersonDataMap().getCoupon());//现金红包
                                        strMoney = String.valueOf(walletBean.getData().getPersonDataMap().getRemainElecNum());
                                        userName = walletBean.getData().getPersonDataMap().getUserName();
                                        Picasso.get().load(BaseContacts.BASE_URL + walletBean.getData()
                                                .getPersonDataMap().getHeadPhoto())
                                                .into(wallet_iv_img);

                                        if (walletBean.getData().getPersonDataMap().getLev() == 0
                                                && walletBean.getData().isIsGCmember() == false) {
                                            wallet_iv_vip0.setImageResource(R.mipmap.free2x);
                                            wallet_tv_time.setVisibility(View.GONE);
                                        } else if (walletBean.getData().isIsGCmember() == true &&
                                                walletBean.getData().getPersonDataMap().getLev() == 0) {
                                            wallet_iv_vip0.setImageResource(R.mipmap.free2x);
                                            wallet_iv_vip1.setImageResource(R.mipmap.vip_gold2x);
                                            wallet_tv_time.setVisibility(View.VISIBLE);
                                            wallet_tv_time.setText("绿色通道到期时间：" + walletBean.getData().getGCmemberExpireTime());
                                        } else if (walletBean.getData().getPersonDataMap().getLev() == 1) {
                                            wallet_iv_vip0.setImageResource(R.mipmap.free2x);
                                            wallet_iv_vip1.setImageResource(R.mipmap.partner_gold2x);
                                            wallet_iv_vip2.setImageResource(R.mipmap.vip_gold2x);
                                            wallet_tv_time.setVisibility(View.GONE);
                                        } else if (walletBean.getData().getPersonDataMap().getLev() == 2 &&
                                                walletBean.getData().getPersonDataMap().getIsSubProxy() == 0) {
                                            wallet_iv_vip0.setImageResource(R.mipmap.free2x);
                                            wallet_iv_vip1.setImageResource(R.mipmap.partner_gold2x);
                                            wallet_iv_vip2.setImageResource(R.mipmap.vip_gold2x);
                                            wallet_tv_time.setVisibility(View.GONE);
                                        } else if (walletBean.getData().getPersonDataMap().getLev() == 2 &&
                                                walletBean.getData().getPersonDataMap().getIsSubProxy() == 1) {
                                            wallet_iv_vip0.setImageResource(R.mipmap.free2x);
                                            wallet_iv_vip1.setImageResource(R.mipmap.partner_gold2x);
                                            wallet_iv_vip2.setImageResource(R.mipmap.vip_gold2x);
                                            wallet_iv_vip3.setImageResource(R.mipmap.quasiagency_gold2x);
                                            wallet_tv_time.setVisibility(View.GONE);
                                        } else if (walletBean.getData().getPersonDataMap().getLev() == 3) {
                                            wallet_iv_vip0.setImageResource(R.mipmap.free2x);
                                            wallet_iv_vip1.setImageResource(R.mipmap.partner_gold2x);
                                            wallet_iv_vip2.setImageResource(R.mipmap.vip_gold2x);
                                            wallet_iv_vip3.setImageResource(R.mipmap.quasiagency_gold2x);
                                            wallet_iv_vip4.setImageResource(R.mipmap.agent_gold2x);
                                            wallet_tv_time.setVisibility(View.GONE);
                                        }
                                    } else if (walletBean.getError_code() == 3) {
                                        Intent intent = new Intent();
                                        intent.setAction("com.broadcast.info_out_line");
                                        sendBroadcast(intent);
                                    } else {
                                        ToastUitl.showShort(walletBean.getError_message());
                                    }

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }
                        });
    }


    @OnClick({R.id.wallet_ll_payment, R.id.assignment_ll_cash, R.id.withdraw_ll_desposit, R.id.documentaryfilm,
            R.id.wallet_iv_level, R.id.record_ll_shopping, R.id.record_ll_red_packet, R.id.record_ll_points,
            R.id.shunt_ll_limit})
    public void onClickWalletPayment(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            /**
             * 充值
             */
            case R.id.wallet_ll_payment:
                intent.setClass(getBaseContext(), RechargeCashActivity.class);
                startActivity(intent);
                break;
            /**
             * 转让
             */
            case R.id.assignment_ll_cash:
                intent.setClass(getBaseContext(), AssignmentCashActivity.class);
                intent.putExtra("strMoney", strMoney);
                startActivity(intent);
                break;
            /**
             * 现金币提现
             */
            case R.id.withdraw_ll_desposit:
                intent.setClass(getBaseContext(), WithdrawDespositActivity.class);
                intent.putExtra("strMoney", strMoney);
                startActivity(intent);
                break;
            /**
             * 记录
             */
            case R.id.documentaryfilm:
                intent.setClass(getBaseContext(), RecordListActivity.class);
                startActivity(intent);
                break;
            /**
             * 我要升级
             */
            case R.id.wallet_iv_level:
                intent.setClass(getBaseContext(), VipLevelActivity.class);
                intent.putExtra("userName",userName);
                startActivity(intent);
                break;
            /**
             * 购物币记录
             */
            case R.id.record_ll_shopping:
                intent.setClass(getBaseContext(), RecordShoppingListActivity.class);
                startActivity(intent);
                break;
            /**
             * 红包记录
             */
            case R.id.record_ll_red_packet:
                intent.setClass(getBaseContext(), RecordRedPachetListActivity.class);
                startActivity(intent);
                break;
            /**`
             * 消费积分记录
             */
            case R.id.record_ll_points:
                intent.setClass(getBaseContext(), RecordPointsListActivity.class);
                startActivity(intent);
                break;
            /**
             * 分流额度
             */
            case R.id.shunt_ll_limit:
                intent.setClass(getBaseContext(), ShuntLimitActivity.class);
                intent.putExtra("typeMoney", tv_shopping_money.getText().toString());
                startActivity(intent);
                break;
        }

    }

    @OnClick(R.id.wallet_iv_back)
    public void onClickBack() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initViewRequest();
    }

}

